package com.pulisher.controller;

import com.pulisher.bean.CourseSubjectAmountBean;
import com.pulisher.bean.CourseSubjectOrderBean;
import com.pulisher.service.CourseStatsService;
import com.pulisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Create 2023/9/11 18:57
 */
@RestController
@RequestMapping("/course")
public class CourseStatsController {

    @Autowired
    CourseStatsService courseStatsService;

    //TODO 1、各学科下单详情
    @RequestMapping(value = "/order",method = RequestMethod.POST)
    public String getCourseOrder(@RequestParam(value = "date",defaultValue = "0") String date){

        if (date.equals("0")){
            date = DateFormatUtil.now().toString();
        }

        List<CourseSubjectOrderBean> orderBeanList = courseStatsService.getCourseOrder(date);
        if (orderBeanList==null||orderBeanList.size()==0){
            return "";
        }

        StringBuilder columns = new StringBuilder();
        columns.append("{\n\"name\": \"学科\",\n\"id\": \"subject_name\"\n},")
                .append("{\n\"name\": \"下单人数\",\n\"id\": \"user_ct\"\n},")
                .append("{\n\"name\": \"订单数\",\n\"id\": \"order_ct\"\n},")
                .append("{\n\"name\": \"订单金额\",\n\"id\": \"order_amount\"\n}");
        StringBuilder rows = new StringBuilder();
        for (int i = 0; i < orderBeanList.size(); i++) {
            CourseSubjectOrderBean orderBean = orderBeanList.get(i);
            rows.append("{\n\"subject_name\": \""+orderBean.getSubject_name()+"\"," +
                    "\n\"user_ct\": \""+orderBean.getUser_ct()+"\"," +
                    "\n\"order_ct\": \""+orderBean.getOrder_ct()+"\"," +
                    "\n\"order_amount\": \""+orderBean.getOrder_amount().setScale(2, BigDecimal.ROUND_HALF_UP)+"\"\n}"
            );
            if (i<orderBeanList.size()-1){
                rows.append(",");
            }
        }

        return "{"+
                "\"status\": 0,"+
                "\"msg\": \"\","+
                "\"data\": {"+
                "\"columns\": ["+columns+
                "],\n"+
                "\"rows\": ["+rows+
                "]"+
                "}"+
                "}";

    }

    //TODO 2、各学科销售额
    @RequestMapping(value = "/amount",method = RequestMethod.POST)
    public String getCourseAmount(@RequestParam(value = "date",defaultValue = "1") String date){
        if (date.equals("1")){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date=dateFormat.format(new Date());
            //String format = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        }

        List<CourseSubjectAmountBean> amountBeanList = courseStatsService.getCourseAmount(date);
        if (amountBeanList==null){
            return "";
        }

        StringBuilder datas = new StringBuilder();
        for (int i = 0; i < amountBeanList.size(); i++) {
            CourseSubjectAmountBean amountBean = amountBeanList.get(i);
            datas.append("{\n\"name\": \""+amountBean.getSubjectName()+"\",\n\"value\": \""+amountBean.getOrderAmount().setScale(2, RoundingMode.HALF_UP)+"\"\n}");
            if (i<amountBeanList.size()-1){
                datas.append(",");
            }
        }

        return "{"+
                "\"status\": 0,"+
                "\"msg\": \"\","+
                "\"data\": ["+datas+
                "]"+
                "}";
    }



}
